<link href="themes/default/default.css" rel="stylesheet" type="text/css" /> 
<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */

function online_user(){
    global $db;
    $sql="select distinct a.user_id,b.user_name,b.fullname
        from _user_logs a, _user_users b
        where a.user_id=b.id and a.on_line='1'";
    $result=$db->query($sql);
    echo "<div class='userlist'><img class='icon_small'
        src='images/ic_online.png' title=\"Guest\"/>
        Guest</div>";
    while ($row=$db->fetch_array($result)){
        extract($row);
        
        echo "<div class='userlist'><img class='icon_small'
        src='images/ic_online.png' title=\"$fullname\"/>
        $user_name</div>";
    }
}
function send_message(){
    global $user_to,$login_status,$user_id;
    if (!$login_status){
        $user_from=2;
    } else {
        $user_from=$user_id;
    }
    $user_to_name=get_user_info_id($user_to, 'fullname');
    echo "Message to : $user_to_name";
    echo "<form action=\"?mod=messenger\" method='post'>
    <input type=hidden name='action' value='send_process'/>
    <input type=hidden name='user_from' value='$user_id'/>    
    <input type=hidden name='user_to' value='$user_to'/>
    <textarea name='message' cols=50 rows=2></textarea>
    <br/>
    <input type='submit' name='submit' value='Submit'/>
    </form>";
}
function send_process(){
    global $db,$user_to,$user_from,$login_status,$message;
    $msg=new messengers();
    if (!empty($user_from)){
        $msg->user_from=$user_from;
    } else {
        $msg->user_from=2;
    }
    
    $msg->user_to=$user_to;
    $msg->message=$message;
    $msg->insert();

    refresh_to("?mod=messenger","Pesan di sampaikan",1);
}
function list_message(){
    global $db,$user_id;
    if (empty($user_id))$user_id=1;
    $sql="select a.* 
    from _messengers a
    where a.user_from=$user_id or a.user_to=$user_id
    order by id asc";
    $result=$db->query($sql);
    $last_user=$user_id;
    $last_from=$user_id;
    while($row=$db->fetch_array($result)){
        extract($row);
        $dari=get_user_info_id($user_from,'fullname');
        $kepada=get_user_info_id($user_to,'fullname');
        if ($user_from==$user_id){
        echo "<div class='chatbox'>";
        if ($last_from!=$user_id){
        echo "<div class='infouser_left'><img class='icon' src='images/chat/userPL04.png'/>$dari ke $kepada $chat_date</div>";
        }
        echo "<div class='chat_message'>$message</div></div>";
        $last_user=$user_to;
        } else {
        echo "<div class='chatbox'>
            <div class='infouser_right'><img class='icon' src='images/chat/userPL05.png'/>$dari ke $kepada $chat_date</div>
        <div class='chat_message'>$message</div></div>";
        $last_from=$user_from;
        
        }
    }
    echo "<form action=\"?mod=messenger\" method='post'>
    <input type=hidden name='action' value='send_process'/>
    <input type=hidden name='user_from' value='$user_id'/>
    <input type=hidden name='user_to' value='$last_user'/>
    <textarea name='message' cols=50 rows=2></textarea>
    <br/>
    <input type='submit' name='submit' value='Submit'/>
    </form>";
}
function chat_box(){
    echo "
<h2>Inherent Online Chat</h2>        
<iframe src='messenger/chat.php' width='638' height='600' frameborder='0' scrolling='auto'></iframe>";
}
global $action;
switch($action){
    case 'user_list':
        online_user();
        break;
    case 'send':
        send_message();
        break;
    case 'send_process':
        send_process();
        break;
    default:
        //list_message();
        chat_box();
}
?>